package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import liuliu.custom.control.edittext.ImageEditText;
import liuliu.custom.control.edittext.SpinnerEditText;
import liuliu.custom.control.picker.DatePickerDialog;
import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.adapter.CommonAdapter;
import liuliu.throughwaring.adapter.ViewHolder;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.model.CodeModel;
import liuliu.throughwaring.model.Key;
import liuliu.throughwaring.model.PoliceModel;

/**
 * Created by liuliu on 2015/11/18   15:55
 * 单位查询
 *
 * @author 柳伟杰
 * @Email 1031066280@qq.com
 */
public class PoliceSearchActivity extends BaseActivity {
    public static PoliceSearchActivity mInstance;
    @CodeNote(id = R.id.police_search_right_menu_draw)
    DrawerLayout right_menu;
    @CodeNote(id = R.id.police_search_toolbar)
    TToolbar mToolbar;
    @CodeNote(id = R.id.right_menu_toolbar)
    TToolbar mRightToolbar;
    @CodeNote(id = R.id.right_menu_listview)
    ListView mRightMenu;
    List<CodeModel> mCodeList;
    CodeModel mCode;
    /*左划菜单布局*/
    @CodeNote(id = R.id.right_menu_unit_search_ll)
    LinearLayout right_menu_unit;
    @CodeNote(id = R.id.right_menu_person_search_ll)
    LinearLayout right_menu_person;
    @CodeNote(id = R.id.right_menu_police_search_ll)
    LinearLayout right_menu_police;
    DatePickerDialog datePickerDialog;
    @CodeNote(id = R.id.right_menu_police_search_record_date_begin, click = "onClick")
    SpinnerEditText record_date_begin;
    @CodeNote(id = R.id.right_menu_police_search_record_date_end, click = "onClick")
    SpinnerEditText record_date_end;
    @CodeNote(id = R.id.right_menu_police_search_name)
    ImageEditText menu_name;
    @CodeNote(id = R.id.right_menu_police_search_id)
    ImageEditText menu_id;//警号
    @CodeNote(id = R.id.right_menu_police_search_tel)
    ImageEditText menu_tel;//电话
    @CodeNote(id = R.id.right_menu_search_button, click = "onClick")
    Button btn_search;
    List mPoliceList;
    PoliceModel mPolice;
    @CodeNote(id = R.id.police_search_main_list)
    ListView mListView;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_police_search);
        mInstance = this;
        initDatas();
        initlist();//加载主页面list
    }

    List<CodeModel> codeList;//企业类型

    //加载数据
    private void initDatas() {
        String[] s = {"不限", "从业人员", "流动人员", "租赁人员"};
        codeList = finalDb.findAllByWhere(CodeModel.class, "codeName='Code_CompanyType'");
        if (codeList.size() > 0) {
            s[0] = "不限";
            for (int i = 0; i < codeList.size(); i++) {
                s[i + 1] = codeList.get(i).getValue();
            }
        }

        mCodeList = new ArrayList<>();
        for (int i = 0; i < s.length; i++) {
            mCode = new CodeModel();
            mCode.setCodeName(s[i]);
            mCodeList.add(mCode);
        }
        mPoliceList = new ArrayList();
        for (int i = 0; i < 500; i++) {
            mPolice = new PoliceModel();
            mPolice.setPoliceName("胡海珍" + i);
            mPoliceList.add(mPolice);
        }
    }

    @Override
    public void initEvents() {
        mToolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                mInstance.finish();
            }
        });
        mToolbar.setRightOnClick(new TToolbar.RightOnClickListener() {
            @Override
            public void rightclick() {
                drawerManager("精确查找");//设置左划菜单的title
                mRightToolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
                    @Override
                    public void leftclick() {
                        drawerManager("");
                    }
                });
                mRightMenu.setVisibility(View.GONE);
                right_menu_unit.setVisibility(View.GONE);
                right_menu_person.setVisibility(View.GONE);
                right_menu_police.setVisibility(View.VISIBLE);
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent();
                intent.setClass(PoliceSearchActivity.this,PoliceDetailActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("PoliceModel", (Serializable) mPoliceList.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.right_menu_police_search_record_date_begin:
                datePickerDialog = new DatePickerDialog(mInstance, "");
                datePickerDialog.datePickerDialog(record_date_begin);
                break;
            case R.id.right_menu_police_search_record_date_end:
                datePickerDialog = new DatePickerDialog(mInstance, "");
                datePickerDialog.datePickerDialog(record_date_end);
                break;
            case R.id.right_menu_search_button://确定查询按钮
                drawerManager("");
                initlist();
                break;
        }
    }

    private void searchPoliceList() {
        String sql = " 1=1 ";
        if (!menu_name.getText().equals("")) {
            sql += " and PoliceName like '%" + menu_name.getText() + "%'";
        }
        if (!menu_id.getText().equals("")) {//警号
            sql += " and PoliceId='" + menu_id.getText() + "'";
        }
        if (!menu_tel.getText().equals("")) {//电话
            sql += " and PolicePhoneNumber='" + menu_tel.getText() + "'";
        }
        if (!record_date_begin.getCenterText().equals("")) {
            sql += " and PoliceCreateTime >= '" + Utils.changDatetoGeshi(record_date_begin.getCenterText()) + "'";
        }
        if (!record_date_end.getCenterText().equals("")) {
            sql += " and PoliceCreateTime<='" + Utils.changDatetoGeshi(record_date_end.getCenterText()) + "'";
        }
        mPoliceList = finalDb.findAllByWhere(PoliceModel.class, sql);
    }

    /*刷新list*/
    private void initlist() {
        searchPoliceList();
        mListView.setAdapter(new CommonAdapter<PoliceModel>(mInstance, mPoliceList,
                R.layout.item_main_police_search) {
            @Override
            public void convert(final ViewHolder holder, final PoliceModel model, int position) {
                holder.setText(R.id.police_search_item_main_name, model.getPoliceName());
                holder.setText(R.id.police_search_item_main_id,model.getPoliceId());
                holder.setText(R.id.police_search_item_main_tel, model.getPolicePhoneNumber());
                holder.setText(R.id.police_search_item_main_create_time,model.getPoliceCreateTime());
            }
        });
    }

    /*开启关闭左划菜单*/
    private void drawerManager(String police) {
        if (right_menu.isDrawerOpen(GravityCompat.END) || police .equals("")) {
            right_menu.closeDrawer(GravityCompat.END);
        } else {
            right_menu.openDrawer(GravityCompat.END);
            mRightToolbar.setCenterTitle(police);
        }
    }
    /**
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (right_menu.isDrawerOpen(GravityCompat.END)) {//划出状态
                right_menu.closeDrawer(GravityCompat.END);
            } else {
                this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUtils.WriteString(Key.DWCX_DATA_END, "");
        mUtils.WriteString(Key.DWCX_UNIT_TYPE, "");
        mUtils.WriteString(Key.DWCX_DATA_BEGIN, "");
        mUtils.WriteString(Key.DWCX_ADDRESS, "");
        mUtils.WriteString(Key.DWCX_NAME, "");
    }
}
